<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\Dashboard;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Column;
use Encore\Admin\Layout\Content;
use Encore\Admin\Layout\Row;
use Encore\Admin\Grid;
use Encore\Admin\Widgets\Form as WForm;
use Encore\Admin\Form;

use Illuminate\Http\Request;
use \Illuminate\Support\Facades\Input;
use App\Models\GameDbConf;
use App\Models\GameMapServer;

use App\Admin\Extra\Search\Html;

class GameDbConfController extends Controller
{

    public function index()
    {
        return $this->anyListDb();
    }
    // 数据库列表
    public function anyListDb(){
        return Admin::content(function (Content $content) {

		    $content->header('游戏数据管理');
		    $content->description('.....');

		    $content->row(Html::append(Html::likeRedirect('area', '区', url()->current()) ));	


			$grid = Admin::grid(GameDbConf::class, function(Grid $grid){
                $grid->model()->where('deleted_at', 0);

                $grid->actions(function($actions)use(&$row){
                    $row = $actions->row;
                    $actions->disableEdit();
                    $actions->disableDelete();
                    $actions->append(Html::button('EDIT', '/admin/game-db-conf/edit?id='.$actions->getKey(), 'btn-goto btn-xs'));
                    $actions->append(Html::button('DEL', '/admin/game-db-conf/api-save?_id='.$actions->getKey().'&deleted_at=1', 'btn-goto btn-danger btn-xs btn-clear'));
                });

			    // 第一列显示id字段，并将这一列设置为可排序列
			    $grid->area('区')->sortable();
                $grid->model()->orderBy('area', 'DESC');
                $grid->host('Host');
                $grid->port('port');
                $grid->database('数据库名');
                $grid->username('用户名');
                $grid->password('密码');

                // 服务当前状态
                $states = [
                    'on'  => ['value' => 1, 'text' => '打开', 'color' => 'success'],
                    'off' => ['value' => 0, 'text' => '关闭', 'color' => 'default'],
                    'url' => url()->to("/admin/game-db-conf/api-save?id={$row->id}"),
                ];

                
                // 设置临时跳转路径 行操作使用 TODO
                //$grid->tmpUrl = url()->to("/admin/game-db-conf/api-save?id=".$grid->getKey()->value()."&status=".(''?0:1));
                $tmp = $grid->status('服务状态')->switch($states)->display(function($value){
                    $this->tmpUrl = url()->to("/admin/game-db-conf/api-save?id=".$this->id."&status=".($value?0:1));
                    return $value;
                });

			    $grid->filter(function ($filter) {

                    // 模糊查询 注意
			        // $filter->where(function($query){
                    //     $aids = \App\Models\Account\PlayerActModel::getAccountByName('', 'playerName');
                    //     //var_dump($aids);exit;
                    //     $query->whereIn('account', $aids?:[]);
                    // }, '角色名称', 'playerName');

                    $filter->equal('area', '区')->placeholder('数据库所在game区');
                    

                    $filter->disableIdFilter();
			    });

            });
        
            $content->row($grid->render());
        });

    }

    // 创建
    public function anyCreate(){
        return $this->anyEdit();
    }

    public function anyEdit(){
        $id = Input::get('id');

        $one = GameDbConf::find($id) ?: new GameDbConf;

        $data['one'] = $one;
        
        return Admin::content(function($content) use($data){
		    $content->header('游戏数据编辑');
            $grid = Admin::Form(GameDbConf::class, function(Form $form)use($data){
                $one = $data['one'];

                $form->text('area', '区')->default($one->area);
                $form->text('host', '服务器ip')->default($one->host);
                $form->text('port', 'port')->default($one->port);
                $form->text('database', '数据库名')->default($one->database);
                $form->text('username', '用户名')->default($one->username ?: 'gbsrv');
                $form->text('password', '密码')->default($one->password ?: '123456');
                $form->text('charset', '字符集')->default($one->charset ?: 'utf8mb4');
                $form->text('prefix', '表前缀')->default($one->prefix ?: 'tb_');
                $form->hidden('_id', 'id')->default($one->id);
                $form->setAction('/admin/game-db-conf/api-save');
            });
            $content->row($grid);
        });

    }

    public function anyApiSave(){
        $data = Input::all();

        $one = GameDbConf::find($data['_id']) ? : new GameDbConf();
        GameDbConf::assignByOut($one, $data);
        
        if (!$one->host || !$one->database) {
            echo json_encode(['code' => 444, 'msg' => 'error param']);
            return ;
        }
        $one->save();
        
        echo json_encode(['code' => 0, 'msg' => 'success', 'data' =>$one->delete_at]);
        
        return redirect('/admin/home/redirect?code=0&redirect='.url()->to('/admin/game-db-conf').'&auto=1');
    }

    // 获取map服务列表
    public function anyMapList()
    {
	    return Admin::content(function (Content $content) {

            $content->header('map服务列表');
            $content->description('.....');

		    //$content->row(Html::button('EDIT', '/admin/game-db-conf/map-create?id='.$actions->getKey(), 'btn-goto btn-xs'));	

			$grid = Admin::grid(GameMapServer::class, function(Grid $grid){

			    // 第一列显示id字段，并将这一列设置为可排序列
			    $grid->mid('mapId');
			    $grid->mapIp('IP');
			    $grid->mapPort('端口');
			    $grid->group('所属组');
			    $grid->created_at('开服时间');
			    $grid->updated_at('更新时间');
			    $grid->status('map服务状态');
			    $grid->roleLimit('服务人数上限');
			    $grid->roleCount('服务当前人数');

                // 服务当前状态
                $states = [
                    'on'  => ['value' => 1, 'text' => '打开', 'color' => 'success'],
                    'off' => ['value' => 0, 'text' => '关闭', 'color' => 'default'],
                    //'url' => url()->to("/admin/game-db-conf/api-save?id={$row->id}"),
                ];

                
                // 设置临时跳转路径 行操作使用 TODO
                //$grid->tmpUrl = url()->to("/admin/game-db-conf/api-save?id=".$grid->getKey()->value()."&status=".(''?0:1));
                $tmp = $grid->status('服务状态')->switch($states)->display(function($value){
                    //$this->tmpUrl = url()->to("/admin/game-db-conf/api-save?id=".$this->id."&status=".($value?0:1));
                    return $value;
                });
                $grid->actions(function($actions)use(&$row){
                    $row = $actions->row;
                    $actions->disableEdit();
                    $actions->disableDelete();
                    $actions->append(Html::button('EDIT', '/admin/game-db-conf/map-edit?id='.$actions->getKey(), 'btn-goto btn-xs'));
                    $actions->append(Html::button('DEL', '/admin/game-db-conf/api-map-save?_id='.$actions->getKey().'&deleted_at=1', 'btn-goto btn-danger btn-xs btn-clear'));
                });

                $grid->disableCreateButton();
    
    			$grid->filter(function ($filter) {
                    $filter->equal('group','组');
                });


            });


            //$content->body(view('admin.charts.test'));
            $content->row($grid->render());
            //$content->body(view('admin.charts.test_echart'));
		
            //$content->body(phpinfo());
        });

    }

    // 编辑 map 服务
    public function anyMapEdit()
    {
        $id = Input::get('id');

        $one = GameMapServer::findOne($id);

        $data['one'] = $one;
        
        return Admin::content(function($content) use($data){
		    $content->header('游戏数据编辑');
            $grid = Admin::Form(GameDbConf::class, function(Form $form)use($data){
                $one = $data['one'];

                $form->text('mid', '区')->default($one['mid']);
                $form->text('mapIp', '服务器ip')->default($one['mapIp']);
                $form->text('mapPort', '服务器端口号')->default($one['mapPort']);
                $form->text('group', '服务组')->default($one['group'] ?: 0);
                $form->hidden('_id', 'id')->default($one['id']);
                $form->setAction('/admin/game-db-conf/api-map-save');
            });
            $content->row($grid);
        });

    }

    public function anyMapSave(){
    }

}
